/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TableEditDialog.java
 *
 * Created on Dec 28, 2009, 1:11:36 AM
 */

package com.vizyon.felis.form;

import com.vizyon.felis.core.Field;
import com.vizyon.felis.core.FieldType;
import com.vizyon.felis.core.Table;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Kamil ÖRS
 */
public class TableEditDialog extends javax.swing.JDialog {

    Table table;

    /** Creates new form TableEditDialog */
    public TableEditDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public TableEditDialog(java.awt.Frame parent, boolean modal,Table table) {
        super(parent,modal);
        this.table = table;
        initComponents();
        setLocationRelativeTo(null);
        initTableFields();
        textTableColor.setBackground(table.getBox().getBgColor());
        textTableName.setText(table.getName());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        textTableName = new javax.swing.JTextField();
        textTableColor = new javax.swing.JTextField();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableFleids = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tablo Düzenle");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Tablo Adı :");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Renk :");

        textTableColor.setEditable(false);
        textTableColor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                textTableColorMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(textTableName, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(textTableColor, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textTableName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textTableColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tableFleids.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Key", "Adı", "Tip", "Uzunluk", "NN", "AI"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tableFleids);
        tableFleids.getColumnModel().getColumn(0).setMinWidth(30);
        tableFleids.getColumnModel().getColumn(0).setPreferredWidth(30);
        tableFleids.getColumnModel().getColumn(0).setMaxWidth(30);
        tableFleids.getColumnModel().getColumn(2).setMinWidth(125);
        tableFleids.getColumnModel().getColumn(2).setPreferredWidth(125);
        tableFleids.getColumnModel().getColumn(2).setMaxWidth(125);
        tableFleids.getColumnModel().getColumn(3).setMinWidth(75);
        tableFleids.getColumnModel().getColumn(3).setPreferredWidth(75);
        tableFleids.getColumnModel().getColumn(3).setMaxWidth(75);
        tableFleids.getColumnModel().getColumn(4).setMinWidth(30);
        tableFleids.getColumnModel().getColumn(4).setPreferredWidth(30);
        tableFleids.getColumnModel().getColumn(4).setMaxWidth(30);
        tableFleids.getColumnModel().getColumn(5).setMinWidth(30);
        tableFleids.getColumnModel().getColumn(5).setPreferredWidth(30);
        tableFleids.getColumnModel().getColumn(5).setMaxWidth(30);

        jTabbedPane1.addTab("Tablo Alanları", jScrollPane1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 481, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 132, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Tablo İlişkileri", jPanel2);

        jButton1.setText("Tamam");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Yeni Alan Ekle");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 320, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        DefaultTableModel model = (DefaultTableModel) tableFleids.getModel();

        Vector vector = new Vector();
        vector.add(false);
        vector.add("alan_adi");
        vector.add(FieldType.typeName(FieldType.VARCHAR_TYPE));
        vector.add(0);
        vector.add(false);
        vector.add(false);

        model.addRow(vector);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       DefaultTableModel model = (DefaultTableModel) tableFleids.getModel();
       table.setFields(new ArrayList<Field>(0));
       for(int i = 0; i < model.getRowCount(); i++) {
           Field field = new Field();
           field.setPrimaryKey(new Boolean(model.getValueAt(i, 0).toString()));
           field.setName(model.getValueAt(i, 1).toString());
           field.setType(new FieldType(FieldType.typeInt(model.getValueAt(i, 2).toString()), new Integer(model.getValueAt(i, 3).toString())));
           field.setNotNull(new Boolean(model.getValueAt(i, 4).toString()));
           field.setAutoIncrement(new Boolean(model.getValueAt(i, 5).toString()));
           field.setTable(table);
           table.getFields().add(field);
       }

       table.setName(textTableName.getText());
       table.getBox().setBgColor(textTableColor.getBackground());
       dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void textTableColorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_textTableColorMouseClicked
        Color color = JColorChooser.showDialog(rootPane, null, textTableColor.getBackground());
        textTableColor.setBackground(color);
    }//GEN-LAST:event_textTableColorMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TableEditDialog dialog = new TableEditDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tableFleids;
    private javax.swing.JTextField textTableColor;
    private javax.swing.JTextField textTableName;
    // End of variables declaration//GEN-END:variables


    private void initTableFields() {
        DefaultTableModel model = (DefaultTableModel) tableFleids.getModel();
        model.getDataVector().removeAllElements();

        for(Field field : table.getFields()) {
            Vector vector = new Vector();
            vector.add(field.isPrimaryKey());
            vector.add(field.getName());
            vector.add(FieldType.typeName(field.getType().getType()));
            vector.add(field.getType().getValue());
            vector.add(field.isNotNull());
            vector.add(field.isAutoIncrement());

            model.addRow(vector);
        }
    }
}
